package com.yang.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yang.domain.Category;
import com.yang.service.CategoryService;
import com.yang.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 分类Controller
 *
 * @author 不是菜鸡爱编程
 * @date 2024-02-18
 */
@RestController
@RequestMapping("/category")
@Api(tags = "分类模块", value = "分类Controller")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询分类列表
     */
    @ApiOperation("查询分类列表")
    @GetMapping("/list")
    public R list(Category category) {
        List<Category> categoryList = categoryService.list(new LambdaQueryWrapper<Category>()
                .eq(ObjUtil.isNotEmpty(category.getId()), Category::getId, category.getId())
                .eq(ObjUtil.isNotEmpty(category.getName()), Category::getName, category.getName())
                .eq(ObjUtil.isNotEmpty(category.getParentId()), Category::getParentId, category.getParentId())
                .eq(ObjUtil.isNotEmpty(category.getSorted()), Category::getSorted, category.getSorted())
                .eq(ObjUtil.isNotEmpty(category.getCreateTime()), Category::getCreateTime, category.getCreateTime())
                .eq(ObjUtil.isNotEmpty(category.getUpdateBy()), Category::getUpdateBy, category.getUpdateBy())
                .eq(ObjUtil.isNotEmpty(category.getUpdateTime()), Category::getUpdateTime, category.getUpdateTime())
                .eq(ObjUtil.isNotEmpty(category.getDelFlag()), Category::getDelFlag, category.getDelFlag()));
        List<Category> treeCategory = categoryList.stream().filter(menu->
                menu.getParentId()== 0
        ).map((menu)->{
            //通过递归找到子分类
            menu.setChildrenList(getChild(menu,categoryList));
            return menu;
        }).sorted((menu1,menu2)->{
            return menu1.getSorted()-menu2.getSorted();
        }).collect(Collectors.toList());
        return R.ok().setData(treeCategory);
    }

    private List<Category> getChild(Category root , List<Category> all) {
        List<Category> child = all.stream().filter(menu->
                Objects.equals(menu.getParentId(), root.getId())).map((menu)->{
            //通过递归找到子分类
            menu.setChildrenList(getChild(menu,all));
            return menu;
        }).sorted((menu1,menu2)->{
            return menu1.getSorted()-menu2.getSorted();
        }).collect(Collectors.toList());
        return child;
    }

    /**
     * 获取分类详细信息
     */
    @ApiOperation("获取分类详细信息")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id) {
        return R.ok().setData(categoryService.getById(id));
    }

    /**
     * 新增分类
     */
    @ApiOperation("新增分类")
    @PostMapping
    public R add(@RequestBody Category category) {
        return R.ok().setData(categoryService.save(category));
    }

    /**
     * 修改分类
     */
    @ApiOperation("修改分类")
    @PutMapping
    public R edit(@RequestBody Category category) {
        return R.ok().setData(categoryService.update(category, new LambdaQueryWrapper<Category>()
                .eq(Category::getId, category.getId())));
    }

    /**
     * 删除分类
     */
    @ApiOperation("删除分类")
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Integer[] ids) {
        return R.ok().setData(categoryService.removeByIds(Arrays.asList(ids)));
    }
}
